package com.fengba.nanzhi.tabbar02;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;

public class GameActivity extends AppCompatActivity {
    private WebView webview;
    private ImageButton Btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String game_id = intent.getStringExtra("game_id");
        String user_id = intent.getStringExtra("user_id");
        if(game_id.equals("7")) {
            this.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 横屏
        }
        setContentView(R.layout.wz_activity_game);
        Btn = findViewById(R.id.game_btn);
        webview = findViewById(R.id.game_wv);
        String url = "http://www.nan-zhi.com/app/api/game/"+game_id+"/"+user_id;
        Toast.makeText(GameActivity.this, game_id, Toast.LENGTH_SHORT).show();
        webview.loadUrl(url);
        webview.setInitialScale(50);
        WebSettings settings = webview.getSettings();
        settings.setUseWideViewPort(true);// 这个很关键
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webview.getSettings().setLoadWithOverviewMode(true);
        //设置是否出现缩放工具
//        webview.getSettings().setBuiltInZoomControls(true);
//        //设置可以支持缩放
//        webview.getSettings().setSupportZoom(true);
//        //扩大比例的缩放
//        webview.getSettings().setUseWideViewPort(true);
        //设置编码为utf-8
        webview.getSettings().setDefaultTextEncodingName("utf-8");
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setAppCacheEnabled(true);
         //设置 缓存模式
         webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
         // 开启 DOM storage API 功能
         webview.getSettings().setDomStorageEnabled(true);
        Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webview = null;
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return false;
    }

//    @Override
//    protected void onResume() {
//        /**
//         * 设置为横屏
//         */
//        if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        }
//        super.onResume();
//    }
}
